﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EnterpriseFund.Common
{
    public class OperateSession
    {
        /// <summary>
        /// 添加session(string类型),有效时间默认20分钟
        /// </summary>
        /// <param name="StrSessionName">session名称</param>
        /// <param name="StrSessionValue">session值</param>
        public static void add(string strSessionName, string strSessionValue)
        {
            HttpContext.Current.Session[strSessionName] = strSessionValue;
            HttpContext.Current.Session.Timeout = 60 * 10;
        }


        /// <summary>
        /// 添加session(string类型),并设置session有效时间
        /// </summary>
        /// <param name="StrSessionName">session名称</param>
        /// <param name="StrSessionValue">session值</param>
        /// <param name="timeOut">session有效时间</param>
        public static void add(string strSessionName, string strSessionValue, int timeOut)
        {
            HttpContext.Current.Session[strSessionName] = strSessionValue;
            HttpContext.Current.Session.Timeout = timeOut;
        }

        /// <summary>
        /// 添加session(List类型),有效时间默认20分钟
        /// </summary>
        /// <typeparam name="T">list的类型</typeparam>
        /// <param name="StrSessionName">session名称</param>
        /// <param name="StrSessionValue">session值</param>
        public static void addList<T>(string strSessionName, List<T> strSessionValue) where T : class
        {
            HttpContext.Current.Session[strSessionName] = strSessionValue;
            HttpContext.Current.Session.Timeout = 60 * 10;
        }


        /// <summary>
        /// 添加session(List类型),并设置session有效时间
        /// </summary>
        /// <typeparam name="T">list的类型</typeparam>
        /// <param name="StrSessionName">session名称</param>
        /// <param name="StrSessionValue">session值</param>
        /// <param name="timeOut">session有效时间</param>
        public static void addList<T>(string strSessionName, List<T> strSessionValue, int timeOut) where T : class
        {
            HttpContext.Current.Session[strSessionName] = strSessionValue;
            HttpContext.Current.Session.Timeout = timeOut;
        }


        /// <summary>
        /// 获取string类型的session，无则返回null值
        /// </summary>
        /// <param name="strSessionName">session名称</param>
        /// <returns></returns>
        public static string get(string strSessionName)
        {
            if (HttpContext.Current.Session[strSessionName] == null)
            {
                return null;
            }
            else
            {
                return HttpContext.Current.Session[strSessionName].ToString();
            }
        }

        /// <summary>
        /// 获取List类型的session，无则返回null值
        /// </summary>
        /// <typeparam name="T">list的类型</typeparam>
        /// <param name="strSessionName">session名称</param>
        /// <returns></returns>
        public static List<T> getList<T>(string strSessionName) where T : class
        {
            if (HttpContext.Current.Session[strSessionName] == null)
            {
                return null;
            }
            else
            {
                return (List<T>)HttpContext.Current.Session[strSessionName];
            }
        }


        /// <summary>
        /// 删除session
        /// </summary>
        /// <param name="strSessionNmae">session名称</param>
        public static void del(string strSessionNmae)
        {
            HttpContext.Current.Session[strSessionNmae] = null;
        }
        public static void SetToCache(object o, string name)
        {
            if (HttpContext.Current.Cache[name + HttpContext.Current.Session.SessionID.ToString()] == null)
            {
                HttpContext.Current.Cache.Insert(name + HttpContext.Current.Session.SessionID.ToString(), o, null, DateTime.MaxValue, TimeSpan.FromMinutes(2000));
            }
            else
            {
                HttpContext.Current.Cache[name + HttpContext.Current.Session.SessionID.ToString()] = o;
            }
        }
        public static object GetFromCache(string name)
        {
            if (HttpContext.Current.Cache[name + HttpContext.Current.Session.SessionID.ToString()] != null)
            {

                return HttpContext.Current.Cache[name + HttpContext.Current.Session.SessionID.ToString()] as object;
            }
            return null;
        }
       
        public static string usertheme = "Aqua";
        public static string compressurl(string u)
        {
            return DevExpress.Web.ASPxClasses.Internal.ResourceManager.GetResourceUrl(null, u);
        }
        public static void SetToCacheRequest(object o, string name)
        {
            if (HttpContext.Current.Request != null)
            {
                if (HttpContext.Current.Cache[name + HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value.ToString()] == null)
                {
                    HttpContext.Current.Cache.Insert(name + HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value.ToString(), o, null, DateTime.MaxValue, TimeSpan.FromMinutes(2000));
                }
                else
                {
                    HttpContext.Current.Cache[name + HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value.ToString()] = o;
                }
            }
        }
        public static object GetFromCacheRequest(string name)
        {
            if (HttpContext.Current.Request.Cookies["ASP.NET_SessionId"]!=null)
            {

                if (HttpContext.Current.Cache[name + HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value.ToString()] != null)
                {

                    return HttpContext.Current.Cache[name + HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value.ToString()] as object;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
        public static void RemoveCache(string name)
        {
            HttpContext.Current.Cache.Remove(name + HttpContext.Current.Session.SessionID.ToString());
        }
        public static void RemoveCacheRequest(string name)
        {
            HttpContext.Current.Cache.Remove(name + HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value.ToString());
        }
    }
}